/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TbedTreeViewPanel.java
 *
 * Created on 28.Ara.2009, 14:10:30
 */
package org.testis.gui.mvc.view.testbed;

import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.controller.testbed.TbedTreeController;
import org.testis.gui.mvc.utilities.TbedNodeRenderer;
import org.testis.gui.mvc.view.AbstractViewPanel;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author selcuk
 */
public class TbedTreeViewPanel extends javax.swing.JPanel implements AbstractViewPanel {

    private TbedTreeController m_controller;
    private TbedMutableTreeNode m_tbedTreeRoot;

    /** Creates new form TbedTreeViewPanel */
    public TbedTreeViewPanel() {
        m_controller = TestBedControllerList.newInstance().getTbedTreeController();
        m_controller.addView(this);
        m_tbedTreeRoot = m_controller.getTbedTreeRoot();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbedDesignTree = new javax.swing.JTree();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        tbedDesignTree.setModel(new DefaultTreeModel(null));
        tbedDesignTree.setCellRenderer(new TbedNodeRenderer());
        loadTree(m_tbedTreeRoot);
        tbedDesignTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbedDesignTreeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbedDesignTree);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                TbedTreeController.TREE_LOAD_PROPERTY)) {
            TbedMutableTreeNode node = (TbedMutableTreeNode) evt.getNewValue();
            loadTree(node);
        } else if (evt.getPropertyName().equals(
                TbedTreeController.TREE_REFRESH_PROPERTY)) {
            tbedDesignTree.updateUI();
        } else if (evt.getPropertyName().equals(
                TbedTreeController.SELECTION_TREE_PATH_PROPERTY)) {
            TreePath path = (TreePath) evt.getNewValue();
            setSelectionTreePath(path);
        } else if (evt.getPropertyName().equals(
                TbedTreeController.ERROR_PROPERTY)) {
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void loadTree(TbedMutableTreeNode root) {
        if (root == null) {
            return;
        }
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        tbedDesignTree.setModel(treeModel);
        tbedDesignTree.setRootVisible(false);
        tbedDesignTree.updateUI();
        expandTree();
    }

    private void expandTree() {
        LinkedList<TreePath> pathList = new LinkedList<TreePath>();
        for (int i = 0; i < tbedDesignTree.getRowCount(); i++) {
            pathList.add(tbedDesignTree.getPathForRow(i));
        }
        for (int i = 0; i < pathList.size(); i++) {
            tbedDesignTree.expandPath(pathList.get(i));
        }
    }

    private void setSelectionTreePath(TreePath path) {
        if (path != null) {
            tbedDesignTree.setSelectionPath(path);
        }
    }

    private TbedMutableTreeNode getSelectedNode() {
        TbedMutableTreeNode currentNode = null;
        TreePath currentSelection = tbedDesignTree.getSelectionPath();
        if (currentSelection != null) {
            currentNode = (TbedMutableTreeNode) (currentSelection.getLastPathComponent());
        }
        return currentNode;
    }

    private void tbedDesignTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbedDesignTreeMouseClicked
        TreePath path = tbedDesignTree.getPathForLocation(evt.getX(),
                evt.getY());
        tbedDesignTree.setSelectionPath(path);
        TbedMutableTreeNode selectedNode = getSelectedNode();
        if (selectedNode != null) {
            m_controller.operationSelectTestBedItem(selectedNode);
            if (evt.getButton() == MouseEvent.BUTTON3) {
                TestBedControllerList.newInstance().getPopupController().operationOpenPopupMenu(
                        selectedNode, evt);
            }
        }
    }//GEN-LAST:event_tbedDesignTreeMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree tbedDesignTree;
    // End of variables declaration//GEN-END:variables

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Test Bed Settings");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new TbedTreeViewPanel());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
